package org.alvin.gencode.design;

import com.lowagie.text.Rectangle;
import com.lowagie.text.*;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.style.RtfParagraphStyle;
import lombok.extern.slf4j.Slf4j;
import org.alvin.gencode.AbstractService;
import org.alvin.gencode.beans.PropConfig;
import org.alvin.gencode.run.code.Table;
import org.alvin.gencode.run.code.*;
import org.alvin.gencode.run.service.JDBC2MbTypeService;
import org.alvin.gencode.run.service.VmFileService;
import org.alvin.mini_inject.annotations.MiniComponent;
import org.alvin.mini_inject.annotations.MiniInject;

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@Slf4j
@MiniComponent
public class GenDocService extends AbstractService {

    @MiniInject
    private CodeDao codeDao;
    @MiniInject
    private PostgreCodeDao postgreCodeDao;

    @MiniInject
    private VmFileService vmFileService;
    @MiniInject
    private JDBC2MbTypeService jdbc2MbTypeService;


    @Override
    public ICodeDao getCodeDaoByType(String dbType) {
        if ("postgre".equals(dbType)) {
            return this.postgreCodeDao;
        }
        return this.codeDao;
    }


    @Override
    public void doCreate(List<Table> tables, PropConfig propConfig) throws SQLException {
        //创建一个word文档，等待填写内容
        Document document = new Document(PageSize.A4);
        try {
            // 创建文件夹
            String outDir = propConfig.getConfigDir().concat(File.separator).concat("dist");
            File dir = new File(outDir);
            dir.mkdirs();
            System.out.println("创建目录：" + dir.getAbsolutePath());
            // 创建文件
            File file = new File(dir, "数据库设计文档.docx");
            if (file.exists() && file.isFile()) {
                file.delete();
            }
            file.createNewFile();
            System.out.println("创建文件：" + file.getAbsolutePath());
            //内容填写完，输出文件
            RtfWriter2.getInstance(document, new FileOutputStream(file));
            //打开 doc
            document.open();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //循环遍历输出所有的表的信息
        for (int i = 0; i < tables.size(); i++) {
            Table table = tables.get(i);
            System.out.println("第" + i + "个表:" + table.getComment());
            List<Field> fList = this.getFields(table,propConfig);
            System.out.println("查询字段数量：" + fList.size());
            if (fList == null || fList.isEmpty()) {
                log.error("沒有查詢到字段");
                continue;
            }
            document = this.docAll(document,table,fList);
        }
        //关闭 doc
        document.close();
    }

    private Document docAll(Document document, Table dbtable, List<Field> fList) {
        try {
            String title = "数据库表结构：" + dbtable.getT_name() + "系统(" + dbtable.getComment() + "表)";
            Paragraph paragraph = new Paragraph(title, RtfParagraphStyle.STYLE_HEADING_1);
            paragraph.setAlignment(0);
            document.add(paragraph);
            com.lowagie.text.Table table = new com.lowagie.text.Table(8);
            table.setWidth(100);
            table.setBorderWidth(Rectangle.NO_BORDER);
            table.setPadding(0);
            table.setSpacing(0);
            Cell cell = null;
            cell = new Cell("字段名");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("数据库列名");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("Type");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("Length");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("PK");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("FK");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("Default");
            cell.setHeader(true);
            table.addCell(cell);

            cell = new Cell("Description");
            cell.setHeader(true);
            table.addCell(cell);

            for (int i = 0; i < fList.size(); i++) {
                Field fi = fList.get(i);
                cell = new Cell(fi.getComment());
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell(fi.getName());
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                String length = "";
                String lenNum = "-";
                if (fi.getLength() != null) {
                    length = "(" + fi.getLength() + ")";
                    lenNum = fi.getLength() + "";
                }

                cell = new Cell(fi.getDbType().toUpperCase() + length);
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell(lenNum);
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell(fi.getColumn_key() != null ? "Y" : "");
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell("");
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell("");
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);

                cell = new Cell("");
                cell.setUseAscender(true);
                cell.setHorizontalAlignment(Cell.ALIGN_CENTER);
                table.addCell(cell);
            }
            document.add(table);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return document;
    }

}
